package com.yang.app.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.yang.app.config.RenderTool;
import com.yang.app.entity.TbUser;
import com.yang.app.entity.User;
import com.yang.app.service.IUserService;
@Controller
@Scope("prototype")
@RequestMapping(value = "/user")
public class TestController {
	
	@Resource
	private IUserService userService;
	
	@RequestMapping(method = RequestMethod.GET)
	public String getAllUser(ModelMap model){
		System.out.println("I'm  coming  ................");
		
		return "index";
		
	}
	
	@RequestMapping(value="/toIndex",method = RequestMethod.GET)
	public String toIndex(ModelMap model){
		//TbUser user = new TbUser();
		//user.setHost("888888");
		//userService.addUser(user);
		System.out.println("kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk");
		return "user";
		
	}

	@RequestMapping(value="/toModel",method = RequestMethod.POST)
	public ModelAndView  get(ModelMap model,HttpServletRequest request){
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("========" + request.getParameter("userName"));
		ModelAndView mv = new ModelAndView("index");
		User user = new User();
		try {
			request.setCharacterEncoding("UTF-8");
			System.out.println(request.getParameter("userName"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		user.setUserName(request.getParameter("userName"));
		user.setPassword(request.getParameter("password"));
		userService.addUser(user);
		List<User> users = userService.getAllUser();
		mv.addObject("users", users);
		return mv;
		
	}
	
	@RequestMapping(value="/delete",method = RequestMethod.GET)
	public ModelAndView  delete(HttpServletResponse response,ModelMap model,HttpServletRequest request){
		ModelAndView mv = new ModelAndView("index");
		Integer id = Integer.parseInt(request.getParameter("id"));
		userService.delete(id);
		List<User> users = userService.getAllUser();
		mv.addObject("users", users);
		return mv;	
		
//		List<String> items = new ArrayList<String>();
//		items.add("a");
//		items.add("ab");
//		String  json = JSON.toJSONString(items);
//		RenderTool.sendHTML(response, json);
//		RenderTool.sendJSON(response, json);
	}
	@RequestMapping(value="/check",method = RequestMethod.GET)
	public ModelAndView check(HttpServletResponse response,ModelMap model,HttpServletRequest request){
		ModelAndView mv = new ModelAndView("index");
		Integer id = Integer.parseInt(request.getParameter("id"));
		User u = new User();
		u.setId(id);
		User user = userService.check(u);
		
		List<User> users = new ArrayList<User>();
		users.add(user);
		//List<User> users = userService.getAllUser();
		mv.addObject("users",users);
		return mv;
	}
	
}
